﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Media;

namespace BeatEmUp.Menu
{
    public class MenuOptionsScreen : MenuScreen
    {
        private GameManager game;
        MouseState oldMouseState;

        SpriteFont volumeFont;

        Texture2D changeToWindowedTexture;
        Texture2D changeToFullScreenTexture;
        Texture2D changeTo800x600Texture;
        Texture2D changeTo640x480Texture;
        Texture2D changeTo1024x768Texture;
        Texture2D goBackTexture;

        Rectangle changeToWindowedRectangle;
        Rectangle changeToFullScreenRectangle;
        Rectangle changeTo800x600Rectangle;
        Rectangle changeTo640x480Rectangle;
        Rectangle changeTo1024x768Rectangle;

        Rectangle goBackRectangle;
        List<Rectangle> list;
        int width;
        int height;
        private int xshift = 0;
        private double ratio = 1;

        public MenuOptionsScreen(GameManager game) :
            base(game)
        {
            this.game = game;
            isTitleVisible = true;
            width = game.graphics.PreferredBackBufferWidth;
            height = game.graphics.PreferredBackBufferHeight-100;
            changeTo1024x768Rectangle = new Rectangle(width / 2 - 50, height / 2 - 50, 100, 50);
            changeTo640x480Rectangle = new Rectangle(width / 2 - 50, height / 2 + 10, 100, 50);
            changeTo800x600Rectangle = new Rectangle(width / 2 - 50, height / 2 +70 , 100, 50);
            changeToFullScreenRectangle = new Rectangle(width / 2 - 50, height / 2 +130, 100, 50);
            changeToWindowedRectangle = new Rectangle(width / 2 - 50, height / 2 + 190, 100, 50);
            goBackRectangle = new Rectangle(width / 2 - 50, height / 2 + 250, 100, 50);
        }

        public void LoadContent(ContentManager content)
        {
            LoadBaseContent(content);
            changeTo1024x768Texture = content.Load<Texture2D>("MenuGraphics/1024");
            changeTo640x480Texture = content.Load<Texture2D>("MenuGraphics/640");
            changeTo800x600Texture = content.Load<Texture2D>("MenuGraphics/800");
            changeToFullScreenTexture = content.Load<Texture2D>("MenuGraphics/fullScreenButton");
            changeToWindowedTexture = content.Load<Texture2D>("MenuGraphics/widnowedButton");
            goBackTexture = content.Load<Texture2D>("MenuGraphics/continueButton");
            volumeFont = content.Load<SpriteFont>("Fonts/Player");
        }

        public void Draw(SpriteBatch sb)
        {
            DrawBaseElements(sb);
            width = game.graphics.PreferredBackBufferWidth;
            height = game.graphics.PreferredBackBufferHeight - 100;

            this.game.SpriteBatch.Draw(changeToWindowedTexture, changeToWindowedRectangle,Color.White);
            this.game.SpriteBatch.Draw(changeToFullScreenTexture, changeToFullScreenRectangle, Color.White);
            this.game.SpriteBatch.Draw(changeTo800x600Texture, changeTo800x600Rectangle, Color.White);
            this.game.SpriteBatch.Draw(changeTo640x480Texture, changeTo640x480Rectangle, Color.White);
            this.game.SpriteBatch.Draw(changeTo1024x768Texture, changeTo1024x768Rectangle, Color.White);
            this.game.SpriteBatch.Draw(goBackTexture, goBackRectangle, Color.White);
            this.game.SpriteBatch.DrawString(
                volumeFont, 
                "" + SoundManager.ReturnMusicAndSoundVolume(),
                new Vector2(0.0f, (float)this.game.graphics.PreferredBackBufferHeight - 100),
                Color.White
                );

        }

        public void scaleAndMoveOptionsScreenButtons(int xShift, int resY, double ratio)
        {
            changeToWindowedRectangle.X = changeToFullScreenRectangle.X = changeTo800x600Rectangle.X = changeTo640x480Rectangle.X = changeTo1024x768Rectangle.X = goBackRectangle.X =
                       xShift;

            changeToWindowedRectangle.Y = (int)(resY / 2.0 - 160.0 * ratio);
            changeToFullScreenRectangle.Y = (int)(resY / 2.0 - 80.0 * ratio);
            changeTo800x600Rectangle.Y = (int)(resY / 2.0);
            changeTo640x480Rectangle.Y = (int)(resY / 2.0 + 80.0 * ratio);
            changeTo1024x768Rectangle.Y = (int)(resY / 2.0 + 160.0 * ratio);
            goBackRectangle.Y = (int)(resY / 2.0 + 240.0 * ratio);

            changeToWindowedRectangle.Width = changeToFullScreenRectangle.Width = changeTo800x600Rectangle.Width = changeTo640x480Rectangle.Width = changeTo1024x768Rectangle.Width = goBackRectangle.Width =
               (int)(150.0 * ratio);
            changeToWindowedRectangle.Height = changeToFullScreenRectangle.Height = changeTo800x600Rectangle.Height = changeTo640x480Rectangle.Height = changeTo1024x768Rectangle.Height = goBackRectangle.Height =
                (int)(78.0 * ratio);
        }

        public void Update(Microsoft.Xna.Framework.GameTime gameTime, MenuMainComponent menu)
        {
            KeyboardState newState = Keyboard.GetState();
            MouseState newMouseState = Mouse.GetState();
            
            //buttons are 150x78 in stock
            switch (this.game.graphics.PreferredBackBufferWidth)
            {
                case 1024:
                    xshift = 512 - 75;
                    ratio = 1;
                    scaleAndMoveOptionsScreenButtons(xshift, 768, ratio);
                    break;

                case 800:
                    xshift = (int)(400.0 - 75.0 * (800.0 / 1024.0));
                    ratio = 800.0 / 1024.0;
                    scaleAndMoveOptionsScreenButtons(xshift, 600, ratio);
                    break;

                case 640:
                    xshift = (int)(320.0 - 75.0 * (640.0 / 1024.0));
                    ratio = 640.0 / 1024.0;
                    scaleAndMoveOptionsScreenButtons(xshift, 480, ratio);
                    break;
            }

            if ((this.isVisibleAndActive == true) &&
                (newMouseState.LeftButton == ButtonState.Pressed) &&
                (!(oldMouseState.LeftButton == ButtonState.Pressed)))
            {
                if (this.changeTo1024x768Rectangle.Contains(new Point(newMouseState.X, newMouseState.Y)))
                {
                    GameManager.customResolution.ChangeResolutionTo(1024, 768);
                }

                if (this.changeTo640x480Rectangle.Contains(new Point(newMouseState.X, newMouseState.Y)))
                {
                    GameManager.customResolution.ChangeResolutionTo(640, 480);
                }

                if (this.changeTo800x600Rectangle.Contains(new Point(newMouseState.X, newMouseState.Y)))
                {
                    GameManager.customResolution.ChangeResolutionTo(800, 600);
                }

                if (this.changeToFullScreenRectangle.Contains(new Point(newMouseState.X, newMouseState.Y)))
                {
                    game.graphics.IsFullScreen = true;
                    game.graphics.ApplyChanges();
                }

                if (this.changeToWindowedRectangle.Contains(new Point(newMouseState.X, newMouseState.Y)))
                {
                    game.graphics.IsFullScreen = false;
                    game.graphics.ApplyChanges();
                }

                if (this.goBackRectangle.Contains(new Point(newMouseState.X, newMouseState.Y)))
                {
                    this.isVisibleAndActive = false;
                    menu.mainScreen.isVisibleAndActive = true;
                }
                for (int i = 0; i < 100000000; i++) ;
            }
            oldMouseState = newMouseState;
        }
    }
}
